package com.gimi.cloud.bbp.manage.controller.prod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.prod.EcProdBrandDTO;
import com.gimi.cloud.bbp.manage.service.prod.EcProdBrandService;
import com.github.pagehelper.PageInfo;

/**
* 品牌相关Controller
* @author lixj
* @Description
* @create 2017-12-19
* @version 1.0
**/
@RestController
@RequestMapping(value = "/brand")
public class EcProdBrandController {
    private static Logger logger = LoggerFactory.getLogger(EcProdBrandController.class);
    private final EcProdBrandService ecProdBrandService;

    @Autowired
    public EcProdBrandController(EcProdBrandService brandService){
        this.ecProdBrandService = brandService;
    }

    /**
     * 商品品牌根据id查询操作
     * @param ecProdBrandDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/list")
    public Response brandList(@RequestBody EcProdBrandDTO ecProdBrandDTO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Response resp = new Response();
        List<EcProdBrandDTO> ecProdBranks = ecProdBrandService.queryList(ecProdBrandDTO,false);
        Map<String,Object> resData = new HashMap<>();
        resData.put("ecProdBranks",ecProdBranks);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
		return resp;
    }

    /**
     * 查询品牌分页列表
     * @param ecProdBrandDTO 查询参数
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/pageList")
    public Response pageList(@RequestBody EcProdBrandDTO ecProdBrandDTO, HttpServletResponse response) throws Exception{
        Response resp = new Response();
        List<EcProdBrandDTO> ecProdBranks = ecProdBrandService.queryList(ecProdBrandDTO,true);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,new PageInfo<>(ecProdBranks));
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
		return resp;
    }

    /**
     * 保存或更新品牌信息
     * @param request
     * @param response
     * @param ecProdBrandDTO 待保存或更新的品牌信息
     * @throws Exception
     */
    @RequestMapping("saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@RequestBody EcProdBrandDTO ecProdBrandDTO) throws Exception{
        Response resp = new Response();
        ecProdBrandService.saveOrUpdate(ecProdBrandDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"保存成功");
		return resp;
    }

    /**
     * 商品品牌删除操作
     * @param ecProdBrandDTO 待删除的品牌数据
     * @param request
     * @param response
     */
    @RequestMapping(value = "/delete")
    public Response brandDelete(@RequestBody EcProdBrandDTO ecProdBrandDTO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("启用/禁用品牌，逻辑启用/禁用，入参："+ ecProdBrandDTO);
        Response resp = new Response();
        ecProdBrandService.changeStatus(ecProdBrandDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"操作成功");
		return resp;
    }

    /**
     * 获取品牌信息
     * @param ecProdBrandDTO 查询参数
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/gotoEditProdBrand")
    public Response gotoEditProdCatModel(@RequestBody EcProdBrandDTO ecProdBrandDTO, HttpServletResponse response) throws Exception {
        Response resp = new Response();
        EcProdBrandDTO newEcProdBrandDTO = ecProdBrandService.load(ecProdBrandDTO,ecProdBrandDTO.getPdId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("prodBrand",newEcProdBrandDTO);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
		return resp;
    }
}
